package com.iotproject.educms.controller;


import com.iotproject.commonutils.R;
import com.iotproject.educms.entity.CrmBanner;
import com.iotproject.educms.entity.vo.CrmBannerQuery;
import com.iotproject.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-11
 */
//给后台管理系统用
@RestController
@RequestMapping("/educms/banneradmin")
//d @CrossOrigin
public class BannerAdminController {
    @Autowired
    CrmBannerService crmBannerService;

    //条件查询
    @PostMapping("pageBanner/{current}/{limit}")
    public R pageBanner(@PathVariable("current") long current, @PathVariable("limit") long limit, @RequestBody(required = false) CrmBannerQuery crmBannerQuery) {
        Page<CrmBanner> crmBannerPage = new Page<>(current, limit);
        QueryWrapper<CrmBanner> crmBannerQueryWrapper = new QueryWrapper<>();

        String title = crmBannerQuery.getTitle();
        String linkUrl = crmBannerQuery.getLinkUrl();
        String begin = crmBannerQuery.getBegin();
        String end = crmBannerQuery.getEnd();

        if (!StringUtils.isEmpty(title)) {
            crmBannerQueryWrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(linkUrl)) {
            crmBannerQueryWrapper.eq("linkUrl", linkUrl);
        }
        if (!StringUtils.isEmpty(begin)) {
            crmBannerQueryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            crmBannerQueryWrapper.le("gmt_create", end);
        }
        crmBannerQueryWrapper.orderByDesc("gmt_create");
        crmBannerService.page(crmBannerPage, crmBannerQueryWrapper);
        long total = crmBannerPage.getTotal();
        List<CrmBanner> records = crmBannerPage.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    //添加banner
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        crmBannerService.save(crmBanner);
        return R.ok();
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public R get(@PathVariable String id) {
        CrmBanner banner = crmBannerService.getById(id);
        return R.ok().data("item", banner);
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner) {
        crmBannerService.updateById(banner);
        return R.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        crmBannerService.removeById(id);
        return R.ok();
    }
}

